package com.rk.app.poll.client.viewer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.rk.app.poll.client.Poll;
import com.rk.app.poll.client.PollMain;
import com.rk.app.poll.client.PollResponse;
import com.rk.app.poll.client.PollService;
import com.rk.app.poll.client.PollServiceAsync;

public class SimplePollViewer extends Composite {

	private DecoratedStackPanel stackPanel = new DecoratedStackPanel();

	private Poll poll;

	private PollResponse[] pollResponseArray;

	public SimplePollViewer(Poll poll) {
		this.poll = poll;
		final PollServiceAsync pollingService = GWT.create(PollService.class);

		pollingService.getPollResponse(poll.getId().toString(), new AsyncCallback<PollResponse[]>() {

			public void onFailure(Throwable caught) {

			}

			public void onSuccess(PollResponse[] result) {
				pollResponseArray = result;
				constructUI();
			}

		});
		initWidget(stackPanel);
	}

	public void constructUI() {
		stackPanel.setWidth("100%");
		String header = PollMain.getPollHeader(poll);

		int row = -1;
		FlexTable pollFlexTable = new FlexTable();
		pollFlexTable.setWidth("100%");
		pollFlexTable.setBorderWidth(1);
		String state = "<font style=\"color:red;font-weight:bold\">Final Result</font>";
		if (poll.isOpen()) {
			state = "<font style=\"color:green;font-weight:bold\">Intermediate Result</font>";
		}
		pollFlexTable.setHTML(++row, 0, state);
		FlexCellFormatter formatter = pollFlexTable.getFlexCellFormatter();
		formatter.setColSpan(row, 0, 3);
		if (pollResponseArray != null && pollResponseArray.length > 0) {
			pollFlexTable.setHTML(++row, 0, "<b>Item</b>");
			pollFlexTable.setHTML(row, 1, "<b>Vote Received</b>");
			pollFlexTable.setHTML(row, 2, "<b>Voter</b> (shown with final result)");
			HashMap<String, List<String>> hashMap = new HashMap<String, List<String>>();
			for (int j = 0; j < pollResponseArray.length; j++) {
				if (!hashMap.containsKey(pollResponseArray[j].getSelectedChoice())) {
					List<String> list = new ArrayList<String>();
					hashMap.put(pollResponseArray[j].getSelectedChoice(), list);
					String uid = pollResponseArray[j].getUserId();
					list.add(uid.substring(0, uid.indexOf("(")));
				} else {
					String uid = pollResponseArray[j].getUserId();
					hashMap.get(pollResponseArray[j].getSelectedChoice()).add(uid.substring(0, uid.indexOf("(")));
				}

			}
			for (Iterator<String> iterator = hashMap.keySet().iterator(); iterator.hasNext();) {
				String choice = iterator.next();
				List<String> list = hashMap.get(choice);
				pollFlexTable.setText(++row, 0, choice);
				pollFlexTable.setText(row, 1, " " + list.size());
				if (poll.isOpen()) {
					pollFlexTable.setText(row, 2, " ");
				} else {
					pollFlexTable.setText(row, 2, list.toString());
				}
			}
		} else {
			pollFlexTable.setText(++row, 0, "There is no vote for this poll yet.");
			formatter.setColSpan(row, 0, 3);
		}
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setWidth("100%");
		hPanel.setStyleName("formPanel");
		hPanel.add(pollFlexTable);
		stackPanel.add(hPanel, header, true);
	}

}
